<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">会员昵称：</span>
                <el-input placeholder="输入会员昵称" v-model.trim="user_name" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">会员UID：</span>
                <el-input placeholder="输入会员UID" v-model.trim="user_id" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">手机号：</span>
                <el-input placeholder="输入手机号" v-model.trim="tel" oninput="value=value.replace(/[^\d]/g,'')" maxlength="11" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">会员等级：</span>
                <el-select v-model="level_id" clearable placeholder="请选择会员等级">
                    <el-option :label="item.level_name" :value="item.grade_id" v-for="item in level_arrs" :key="item.grade_id" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">会员状态：</span>
                <el-select v-model="user_type" clearable placeholder="请选择会员状态">
                    <el-option label="正常" value="0" />
                    <el-option label="黑名单" value="1" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">标签分组：</span>
                <el-select v-model="tag_id" clearable placeholder="请选择标签分组">
                    <el-option :label="item.title" :value="item.tag_id" v-for="item in tag_arrs" :key="item.tag_id" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">注册日期：</span>
                <el-date-picker
                    v-model="area_time"
                    type="daterange"
                    unlink-panels
                    value-format="YYYY-MM-DD"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                />
            </div>
            <div class="task-one__btn task-wrap-btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
            </div>
        </div>

        <div class="table-mb">
            <el-button type="default" plain @click="clickTag">修改标签</el-button>
            <el-button type="default" plain @click="clicSetLevel(2)">修改等级</el-button>
            <!-- <el-button type="default" plain @click="clickUpdateExp">调整成长值</el-button>
            <el-button type="default" plain @click="clickCoupon(2)">送优惠券</el-button> -->
            <el-popconfirm title="冻结后，对应会员不能再登录小程序，可以随时解冻。请确定是否冻结？" width="400" confirm-button-text="确定" cancel-button-text="取消" @confirm="clickBlack">
                <template #reference>
                    <el-button type="default" plain class="theme-btn">批量冻结</el-button>
                </template>
            </el-popconfirm>
            <el-popconfirm title="删除后，对应会员会在后台移除且从此不能再登录小程序，如果要拉黑请点击”冻结“操作，请确定是否删除？" width="400" confirm-button-text="确定" cancel-button-text="取消" @confirm="allDel">
                <template #reference>
                    <el-button type="default" plain class="theme-btn">批量删除</el-button>
                </template>
            </el-popconfirm>
        </div>
        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
            @selection-change="handleAllSelected"
        >
            <el-table-column type="selection" width="55" fixed="left" />
            <el-table-column label="会员信息" width="420" fixed="left">
                <template #default="{ row }">
                    <div class="table-lr">
                        <el-image class="table-lr__img img-round" loading="lazy" :src="proxy.imgUrl + row.face + '!img_400'" fit="cover" v-if="row.face" />
                        <div class="table-lr__right">
                            <div>
                                <p class="lr-title line-one">{{ row.nickname }}</p>
                            </div>
                            <p class="lr-text">UID：{{ row.uid }}</p>
                            <p class="lr-text">等级：{{ row.user_grade ? row.user_grade.level_name : "暂无" }}</p>
                            <p class="lr-text">
                                标签：<el-tag>{{ row.user_tag ? row.user_tag.title : "暂无" }}</el-tag>
                            </p>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="统计" min-width="300">
                <template #default="{ row }">
                    <div class="statistics-cont">
                        <p>消费：<em>￥</em>{{ row.total_pay || 0 }}</p>
                        <p>余额：<em>￥</em>{{ row.money || 0 }}</p>
                        <p>授信额度：<em>￥</em>{{ row.credit || 0 }}</p>
                        <!-- <p>推荐会员数：{{ row.invite_num || 0 }}个</p> -->
                        <p>订单数：{{ row.orders || 0 }}个</p>
                        <p>成长值：{{ row.exp_point || 0 }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="created_at_label" label="注册时间" min-width="160"></el-table-column>
            <el-table-column label="操作" width="270" class-name="operation-nav" fixed="right">
                <template #default="scope">
                    <el-button link type="primary" class="theme-btn" @click="clickDetail(scope.row)">详情</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickEdit(scope.row)">编辑</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickFrozen(scope.row, scope.$index)">
                        {{ scope.row.black_status == 0 ? "冻结" : "解冻" }}
                    </el-button>
                    <el-button link type="primary" class="theme-btn" @click="clicSetLevel(1, scope.row)">设置等级</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickBalance(scope.row)">调整余额</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickOrder(scope.row)">创建订单</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickUpdateExp(scope.row)">调整成长值</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickCoupon(1, scope.row)">发放优惠券</el-button>
                    <el-button link type="primary" class="theme-btn" @click="clickCredit(scope.row)">调整授信额度</el-button>
                    <!-- <el-popconfirm title="是否确定删除？" confirm-button-text="确定" cancel-button-text="取消" @confirm="clickDel(scope.row)">
                        <template #reference>
                            <el-button link type="primary" class="theme-btn">删除</el-button>
                        </template>
                    </el-popconfirm> -->
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>

        <!-- 调整余额 -->
        <el-dialog v-model="dialogBalance" title="调整余额" width="640px" :append-to-body="true" :destroy-on-close="true" @close="closeBalance">
            <detail-item title="会员UID" :value="itemObjs.uid" bottom="16px" />
            <detail-item title="当前余额" :value="itemObjs.money" bottom="12px" />
            <detail-item class="exp-detail" bottom="16px" title="调整类型">
                <el-radio-group v-model="balance_type">
                    <el-radio label="1">增加</el-radio>
                    <el-radio label="2">减少</el-radio>
                </el-radio-group>
            </detail-item>
            <detail-item class="exp-detail" bottom="16px" title="调整余额">
                <el-input v-model.trim="balances" oninput="value=value.replace(/[^\d.]/g,'')" clearable placeholder="请输入需调整的余额" style="width: 330px" />
            </detail-item>
            <detail-item class="exp-detail" bottom="6px" title="调整备注">
                <el-input v-model.trim="balances_remarks" type="textarea" clearable :rows="3" style="width: 330px" placeholder="请输入调整备注(必填)" />
            </detail-item>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmBalance">确认</el-button>
                    <el-button @click="closeBalance">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 修改标签 -->
        <el-dialog v-model="dialogTag" title="修改标签" width="640px" :append-to-body="true" :destroy-on-close="true">
            <div>
                <detail-item title="标签分组" bottom="10px">
                    <el-radio-group v-model="radio_tag">
                        <el-radio :label="item.tag_id" v-for="item in tag_arrs" :key="item.tag_id">{{ item.title }}</el-radio>
                    </el-radio-group>
                </detail-item>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmTag">确认</el-button>
                    <el-button @click="dialogTag = false">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 调整成长值 -->
        <el-dialog v-model="dialogExp" title="调整成长值" width="580px" :append-to-body="true" :destroy-on-close="true" @close="closeExp">
            <detail-item title="会员UID" :value="itemObjs.uid" bottom="16px" />
            <detail-item title="成长值" :value="itemObjs.exp_point" bottom="12px" />
            <detail-item class="exp-detail" bottom="16px" title="调整类型">
                <el-radio-group v-model="exp_type">
                    <el-radio label="1">增加</el-radio>
                    <el-radio label="2">减少</el-radio>
                </el-radio-group>
            </detail-item>
            <detail-item class="exp-detail" title="成长值">
                <el-input v-model.trim="exp_num" oninput="value=value.replace(/[^\d]/g,'')" clearable placeholder="请输入成长值" style="width: 330px" />
            </detail-item>
            <detail-item class="exp-detail" bottom="6px" title="备注">
                <el-input v-model.trim="exp_remarks" type="textarea" clearable :rows="3" style="width: 330px" placeholder="请输入备注" />
            </detail-item>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmExp">确认</el-button>
                    <el-button @click="closeExp">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 调整授信额度 -->
        <el-dialog v-model="dialogCredit" title="调整授信额度" width="640px" :append-to-body="true" :destroy-on-close="true" @close="closeCredit">
            <detail-item title="会员UID" :value="itemObjs.uid" bottom="16px" />
            <detail-item title="授信额度" :value="itemObjs.credit" bottom="12px" />
            <detail-item class="exp-detail" bottom="16px" title="调整类型">
                <el-radio-group v-model="credit_type">
                    <el-radio label="1">增加</el-radio>
                    <el-radio label="2">减少</el-radio>
                </el-radio-group>
            </detail-item>
            <detail-item class="exp-detail" title="授信额度">
                <el-input v-model.trim="credit_num" oninput="value=value.replace(/[^\d.]/g,'')" clearable placeholder="请输入授信额度" style="width: 330px" />
            </detail-item>
            <detail-item class="exp-detail" bottom="6px" title="调整备注">
                <el-input v-model.trim="credit_remarks" type="textarea" clearable :rows="3" style="width: 330px" placeholder="请输入调整备注(必填)" />
            </detail-item>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmCredit">确认</el-button>
                    <el-button @click="closeCredit">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 修改等级和单个设置等级 -->
        <el-dialog v-model="dialogLevel" title="修改会员等级" width="580px" :append-to-body="true" :destroy-on-close="true" @close="closeLevel">
            <detail-item title="会员UID" :value="itemObjs.uid" bottom="16px" v-if="dialogLevelType == 1" />
            <detail-item title="当前等级" :value="itemObjs.user_grade ? itemObjs.user_grade.level_name : '暂无'" bottom="16px" v-if="dialogLevelType == 1" />
            <detail-item class="exp-detail" bottom="6px" title="选择等级">
                <el-select v-model="level_dialog_id" clearable placeholder="请选择会员等级">
                    <el-option :label="item.level_name" :value="item.grade_id" v-for="item in level_arrs" :key="item.grade_id" />
                </el-select>
            </detail-item>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmLevel">确认</el-button>
                    <el-button @click="closeLevel">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 批量和单个发放优惠券 -->
        <el-dialog
            v-model="dialogCoupon"
            :title="coupon_type == 1 ? '送优惠券' : '批量发放优惠券'"
            width="520px"
            :append-to-body="true"
            :destroy-on-close="true"
            @close="closeCoupon"
        >
            <detail-item title="会员UID" :value="itemObjs.uid" bottom="16px" v-if="coupon_type == 1" />
            <detail-item class="exp-detail" bottom="16px" title="选择优惠券">
                <el-select v-model="coupon_id" clearable placeholder="请选择优惠券" style="width: 280px">
                    <el-option :label="`${item.title}, 库存${item.sku}`" :value="item.coupon_id" v-for="item in coupon_arrs" :key="item.coupon_id" />
                </el-select>
            </detail-item>
            <detail-item class="exp-detail" title="发放数量">
                <el-input v-model.trim="coupon_num" oninput="value=value.replace(/[^\d]/g,'')" clearable placeholder="请输入发放数量" style="width: 280px" />
            </detail-item>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" :loading="form_loading" @click="confirmCoupon">确认</el-button>
                    <el-button @click="closeCoupon">取消</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 编辑 -->
        <el-dialog v-model="dialogEdit" title="编辑会员信息" width="580px" :append-to-body="true" :destroy-on-close="true" @close="closeEdit">
            <el-form ref="ruleRef" :model="ruleForm" :rules="ruleCheck" label-width="120px">
                <el-form-item label="会员姓名" prop="name">
                    <el-input v-model.trim="ruleForm.name" clearable placeholder="请输入会员姓名" />
                </el-form-item>
                <el-form-item label="联系手机" prop="tel">
                    <el-input v-model.trim="ruleForm.tel" oninput="value=value.replace(/[^\d]/g,'')" maxlength="11" clearable placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model.trim="ruleForm.remarks" type="textarea" clearable :rows="4" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item>
                    <div class="flexs" style="margin-top: 20px">
                        <el-button type="primary" :loading="form_loading" @click="submitForm(ruleRef)">确认</el-button>
                        <el-button @click="resetForm(ruleRef)">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch, reactive, onBeforeUnmount } from "vue"
import { useRouter, onBeforeRouteLeave } from "vue-router"
import { Search } from "@element-plus/icons-vue"
import { useBaseStore } from "@store/base"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"
import DetailItem from "@components/DetailItem.vue"

const store = useBaseStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

onMounted(() => {
    getLevelAndTag()
})

onBeforeUnmount(() => {
    itemObjs.value = null
    ruleRef.value = null
})

// 离开-可以利用这个触发删除页面缓存操作
onBeforeRouteLeave((to, from, next) => {
    if (to.name === "MemberListSub" || to.name === "MemberDetail") {
        store.setInclude("MemberList")
    } else {
        store.delInclude("MemberList")
    }
    next()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, search_btn, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
        uid: user_id.value,
        so: user_name.value,
        mobile: tel.value,
        black_status: user_type.value,
        grade_id: level_id.value,
        tag_id: tag_id.value,
        s_time: area_time.value ? area_time.value[0] : "",
        l_time: area_time.value ? area_time.value[1] : "",
    }
    getdata("user/index", params)
}

/**
 * 搜索
 * @param {String} user_name 会员名称
 * @param {String} user_id 会员ID
 * @param {String} area_time 注册时间段
 * @param {String} tel 手机号
 * @param {String} user_type 会员状态，[0:正常,1:已拉黑]
 */
const user_name = ref("")
const user_id = ref("")
const area_time = ref("")
const tel = ref("")
const user_type = ref("")
const handleSearch = () => {
    search_btn.value = true
    pubRefreshParams(getList)
}

/**
 * 会员等级和标签
 * @param {String} level_id 会员等级ID
 * @param {String} tag_id 标签ID
 */
const level_id = ref("")
const level_arrs = ref([])
const tag_id = ref("")
const tag_arrs = ref([])
function getLevelAndTag() {
    proxy.$axios
        .get("user/search")
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                tag_arrs.value = result.tag_list
                level_arrs.value = result.grade_list
                getList()
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}

const itemObjs = ref({}) // 公共数据项
const form_loading = ref(false) // 公共表单按钮提交状态

/**
 * 编辑会员信息
 * @param {Boolean} dialogEdit 弹窗状态
 * @param {Array} ruleRef 表单规则
 * @param {Object} ruleForm  表单字段
 * @param {Object} ruleCheck  表单提示
 */
const dialogEdit = ref(false)
const ruleRef = ref(null)
const ruleForm = reactive({
    id: "",
    name: "",
    tel: "",
    remarks: "",
})
const ruleCheck = reactive({
    name: [{ required: true, message: "请输入会员姓名", trigger: "blur" }],
    tel: [{ required: true, validator: proxy.$util.validPhone, trigger: "blur" }],
})
const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid, fields) => {
        if (valid) {
            form_loading.value = true
            let data = {
                uid: ruleForm.id,
                name: ruleForm.name,
                mobile: ruleForm.tel,
                intro: ruleForm.remarks,
            }
            proxy.$axios
                .post("user/update", data)
                .then((res) => {
                    form_loading.value = false
                    if (res.data.code == 0) {
                        dialogEdit.value = false
                        proxy.$message({ message: "编辑成功！", type: "success" })
                        pubParams(getList)
                    } else {
                        proxy.$message({ message: res.data.msg, type: "error" })
                    }
                })
                .catch((err) => {
                    form_loading.value = false
                })
        }
    })
}
const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
const clickEdit = (row) => {
    dialogEdit.value = true
    setTimeout(() => {
        ruleForm.id = row.uid
        ruleForm.name = row.name
        ruleForm.tel = row.mobile
        ruleForm.remarks = row.intro || ""
    }, 50)
}
const closeEdit = () => {
    resetForm(ruleRef.value)
    dialogEdit.value = false
    form_loading.value = false
}

// 调整余额
const dialogBalance = ref(false)
const balances = ref("")
const balances_remarks = ref("")
const balance_type = ref("1")
const clickBalance = (row) => {
    itemObjs.value = row
    dialogBalance.value = true
}
const confirmBalance = proxy.$util.throttle(() => {
    let regu = /^\d+(\.\d{1,2})?$/
    if (!balances.value || !regu.test(balances.value)) {
        proxy.$message({ message: "输入的余额格式错误", type: "warning" })
        return
    }
    if (balance_type.value == 2 && itemObjs.value.money - balances.value < 0) {
        proxy.$message({ message: "减少的余额不能大于现有余额！", type: "warning" })
        return
    }
    if (balances_remarks.value == '') {
        proxy.$message({ message: "请输入调整备注", type: "warning" })
        return
    }
    form_loading.value = true
    proxy.$axios
        .post("user/money", {
            uid: itemObjs.value.uid,
            type: balance_type.value,
            money: balances.value,
            intro: balances_remarks.value,
        })
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                dialogBalance.value = false
                proxy.$message({ message: "调整余额成功！", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}, 500)
const closeBalance = () => {
    balances.value = ""
    balances_remarks.value = ""
    dialogBalance.value = false
    form_loading.value = false
}

// 修改授信额度
const dialogCredit = ref(false)
const credit_num = ref("")
const credit_remarks = ref("")
const credit_type = ref("1")
const clickCredit = (row) => {
    itemObjs.value = row
    dialogCredit.value = true
}
const confirmCredit = proxy.$util.throttle(() => {
    let regu = /^\d+(\.\d{1,2})?$/
    if (!credit_num.value || !regu.test(credit_num.value)) {
        proxy.$message({ message: "授信额度格式错误", type: "warning" })
        return
    }
    if (credit_type.value == 2 && itemObjs.value.credit - credit_num.value < 0) {
        proxy.$message({ message: "减少的授信额度不能大于现有授信额度！", type: "warning" })
        return
    }
    if (credit_remarks.value == '') {
        proxy.$message({ message: "请输入调整备注", type: "warning" })
        return
    }
    form_loading.value = true
    proxy.$axios
        .post("user/credit", {
            uid: itemObjs.value.uid,
            type: credit_type.value,
            money: credit_num.value,
            intro: credit_remarks.value,
        })
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                dialogCredit.value = false
                proxy.$message({ message: "调整授信额度成功", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}, 500)
const closeCredit = () => {
    form_loading.value = false
    credit_num.value = ""
    credit_remarks.value = ""
    dialogCredit.value = false
}

// 创建订单
const clickOrder = (row) => {
    router.push({
        path: "/Member/MemberCreateOrder",
        query: {
            uid: row.uid,
        },
    })
}

// 批量操作前置判断
function pubBatch() {
    let flag = true
    if (ids.value.length == 0) {
        proxy.$message({ message: "请至少选择一个会员", type: "warning" })
        flag = false
    }
    return flag
}

/**
 * 修改标签
 * @param {Boolean} dialogTag 弹窗状态
 * @param {Number} radio_type 标签值
 */
const dialogTag = ref(false)
const radio_tag = ref("")
const clickTag = () => {
    if (pubBatch()) {
        dialogTag.value = true
    }
}
const confirmTag = () => {
    if (radio_tag.value == "") {
        proxy.$message({ message: "请选择会员标签类型", type: "warning" })
        return
    }
    form_loading.value = true
    proxy.$axios
        .post("user/batchTag", {
            user: ids.value,
            tag_id: radio_tag.value,
        })
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                dialogTag.value = false
                radio_tag.value = ""
                proxy.$message({ message: "修改标签成功！", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}

/**
 * 批量修改等级和设置等级
 * @param {Boolean} dialogLevel 弹窗状态
 * @param {Number} dialogLevelType 等级弹窗类型，1是单个设置等级，2是批量设置等级
 * @param {Number} level_dialog_id 等级值
 * @param {Function} confirmLevel 确认
 */
const dialogLevel = ref(false)
const level_dialog_id = ref("")
const dialogLevelType = ref("")
const clicSetLevel = (type, row = null) => {
    dialogLevelType.value = type
    if (ids.value.length == 0 && dialogLevelType.value == 2) {
        proxy.$message({ message: "请至少选择一个会员", type: "warning" })
        return
    }
    row != null && (itemObjs.value = row)
    dialogLevel.value = true
}
const confirmLevel = () => {
    if (!level_dialog_id.value) {
        proxy.$message({ message: "请选择需要修改的等级", type: "warning" })
        return
    }
    form_loading.value = true
    let datas = {
        grade_id: level_dialog_id.value,
    }
    if (dialogLevelType.value == 1) {
        datas.uid = itemObjs.value.uid
    } else {
        datas.user = ids.value
    }
    let url = dialogLevelType.value == 1 ? "user/grade" : "user/batchGrade"
    proxy.$axios
        .post(url, datas)
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                dialogLevel.value = false
                proxy.$message({ message: "设置成功", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}
const closeLevel = () => {
    level_dialog_id.value = ""
    dialogLevel.value = false
    itemObjs.value = {}
    form_loading.value = false
}

/**
 * 批量和单个发放优惠券
 * @param {Boolean} dialogCoupon 弹窗状态
 * @param {Number} coupon_type 等级弹窗类型，1是单个，2是批量
 * @param {Number} coupon_id 优惠券ID
 * @param {Number} coupon_num 发放数量
 * @param {Function} confirmCoupon 确认
 */
const dialogCoupon = ref(false)
const coupon_id = ref("")
const coupon_num = ref(1)
const coupon_type = ref("1")
const clickCoupon = (type, row = null) => {
    coupon_type.value = type
    if (ids.value.length == 0 && coupon_type.value == 2) {
        proxy.$message({ message: "请至少选择一个会员", type: "warning" })
        return
    }
    row != null && (itemObjs.value = row)
    dialogCoupon.value = true
    getCoupon()
}
const confirmCoupon = proxy.$util.throttle(() => {
    if (!coupon_id.value) {
        proxy.$message({ message: "请选择优惠券", type: "warning" })
        return
    }
    if (!coupon_num.value) {
        proxy.$message({ message: "请输入发放数量", type: "warning" })
        return
    }
    form_loading.value = true
    let datas = {
        uid: itemObjs.value.uid,
        coupon_id: coupon_id.value,
        send_number: coupon_num.value,
    }
    let url = coupon_type.value == 1 ? "user/coupon" : ""
    proxy.$axios
        .post(url, datas)
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                proxy.$message({ message: "赠送优惠券成功", type: "success" })
                dialogCoupon.value = false
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}, 500)
const closeCoupon = () => {
    dialogCoupon.value = false
    coupon_num.value = 1
    itemObjs.value = {}
    form_loading.value = false
}
const coupon_arrs = ref([
    {
        text: "满100可用",
        coupon_id: 1,
    },
    {
        text: "5元",
        coupon_id: 2,
    },
])
function getCoupon() {
    proxy.$axios
        .get("user/coupon")
        .then((res) => {
            if (res.data.code == 0) {
                coupon_arrs.value = res.data.data
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}

// 调整成长值
const dialogExp = ref(false)
const exp_num = ref("")
const exp_type = ref("1")
const exp_remarks = ref("")
const clickUpdateExp = (row) => {
    itemObjs.value = row
    dialogExp.value = true
}
const confirmExp = proxy.$util.throttle(() => {
    let regu = /^\d+$/
    if (!exp_num.value || !regu.test(exp_num.value)) {
        proxy.$message({ message: "输入的成长值内容有误", type: "warning" })
        return
    }
    if (exp_type.value == 2 && itemObjs.value.exp_point - exp_num.value < 0) {
        proxy.$message({ message: "减少的成长值不能大于现有成长值！", type: "warning" })
        return
    }
    if (!exp_remarks.value) {
        proxy.$message({ message: "请输入备注", type: "warning" })
        return
    }
    form_loading.value = true
    proxy.$axios
        .post("user/expPoint", {
            uid: itemObjs.value.uid,
            type: exp_type.value,
            exp_point: exp_num.value,
            intro: exp_remarks.value,
        })
        .then((res) => {
            form_loading.value = false
            if (res.data.code == 0) {
                proxy.$message({ message: "调整成长值成功！", type: "success" })
                dialogExp.value = false
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            form_loading.value = false
        })
}, 500)
const closeExp = () => {
    exp_num.value = ""
    exp_remarks.value = ""
    dialogExp.value = false
    form_loading.value = false
}

// 加黑名单
const clickBlack = () => {
    if (pubBatch()) {
        proxy.$axios
            .post("user/batchBlack", {
                user: ids.value,
            })
            .then((res) => {
                if (res.data.code == 0) {
                    proxy.$message({ message: "设置成功", type: "success" })
                    pubParams(getList)
                } else {
                    proxy.$message({ message: res.data.msg, type: "error" })
                }
            })
            .catch((err) => {})
    }
}

// 冻结/解冻
const clickFrozen = (row, index) => {
    proxy.$axios
        .post("user/black", {
            uid: row.uid,
            black_status: row.black_status == 0 ? 1 : 0,
        })
        .then((res) => {
            if (res.data.code == 0) {
                proxy.$message({ message: "设置成功", type: "success" })
                arr_data.value[index].black_status = row.black_status == 0 ? 1 : 0
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}

/**
 * 批量删除
 * @param {Array} ids  选中数据的集合
 */
const ids = ref([])
const handleAllSelected = (val) => {
    let arrs = []
    for (let i = 0; i < val.length; i++) {
        arrs.push(val[i].uid)
    }
    ids.value = arrs
}
// 删除单个
// const clickDel = (row) => {
//     postDel(row.uid)
// }
const allDel = () => {
    pubBatch() && postDel(ids.value)
}
// 删除接口
function postDel(params) {
    proxy.$axios
        .post("user/delete", {
            user: params,
        })
        .then((res) => {
            if (res.data.code == 0) {
                proxy.$message({ message: "删除成功", type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}

// 查看详情
const clickDetail = (row) => {
    router.push({
        path: "/Member/MemberDetail",
        query: {
            uid: row.uid,
        },
    })
}
</script>
<script>
export default {
    name: "MemberList",
}
</script>

<style lang="less" scoped>
.statistics-cont {
    font-size: @font-size-md;
    em {
        font-size: 13px;
    }
}
:deep(.el-radio) {
    height: auto;
    margin-bottom: @margin-sm;
}
.exp-detail {
    :deep(.detail-item__left) {
        line-height: 32px;
    }
    :deep(.el-input__wrapper),
    :deep(.el-input__inner) {
        height: 36px;
        line-height: 36px;
    }
    :deep(.el-radio) {
        line-height: 32px;
        height: auto;
        margin-bottom: 0;
    }
}
</style>
